﻿using Microsoft.AspNetCore.Mvc;
using star_questionnair.Services.impl;
using System.Threading.Tasks;

namespace star_questionnair.Controllers;

[Route("notify")]
[ApiController]
public class EmailController : ControllerBase
{
    private readonly EmailService _emailService;

    // 注入 EmailService
    public EmailController(EmailService emailService)
    {
        _emailService = emailService;
    }

    // 发送简单邮件接口
    [HttpPost("/notify/sendSimpleEmail")]
    public async Task<IActionResult> SendSimpleEmail([FromBody] SendEmailRequest request)
    {
        if (string.IsNullOrEmpty(request.To) || string.IsNullOrEmpty(request.Subject) ||
            string.IsNullOrEmpty(request.Text))
        {
            return BadRequest("收件人、主题或内容不能为空。");
        }

        try
        {
            await _emailService.SendSimpleMessageAsync(request.Username, request.To, request.Subject, request.Text);
            return Ok("邮件发送成功");
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"邮件发送失败: {ex.Message}");
        }
    }

    // 发送指定发件人、收件人、主题和内容的邮件接口
    [HttpPost("/notify/sendCustomEmail")]
    public async Task<IActionResult> SendCustomEmail([FromBody] SendCustomEmailRequest request)
    {
        if (string.IsNullOrEmpty(request.From) || string.IsNullOrEmpty(request.To) ||
            string.IsNullOrEmpty(request.Subject) || string.IsNullOrEmpty(request.Text))
        {
            return BadRequest("发件人、收件人、主题或内容不能为空。");
        }

        try
        {
            await _emailService.SendEmailAsync(request.From, request.To, request.Subject, request.Text);
            return Ok("邮件发送成功");
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"邮件发送失败: {ex.Message}");
        }
    }
}

// 简单邮件发送请求数据模型
public class SendEmailRequest
{
    public string Username { get; set; } // 收件人用户名
    public string To { get; set; } // 收件人邮箱地址
    public string Subject { get; set; } // 邮件主题
    public string Text { get; set; } // 邮件内容
}

// 自定义邮件发送请求数据模型
public class SendCustomEmailRequest
{
    public string From { get; set; } // 发件人邮箱
    public string To { get; set; } // 收件人邮箱
    public string Subject { get; set; } // 邮件主题
    public string Text { get; set; } // 邮件内容
}